﻿'  __ _                 
' / _| |                
'| |_| |_   _ _ __ _ __ 
'|  _| | | | | '__| '__|
'| | | | |_| | |  | |   
'|_| |_|\__,_|_|  |_|

'Copyright (C) 2012, Rafal Marek Chmiel
'All rights reserved. http://flurr.codeplex.com/

'Copyright (C) 2010, Stephen Perelson (WinTumblr)
'All rights reserved. http://www.feedfeast.com/wintumblr/

' Redistribution and use in source and binary forms, with or without modification, are 
' permitted provided that the following conditions are met:
'
' - Redistributions of source code must retain the above copyright notice, this list 
'   of conditions and the following disclaimer.
' - Redistributions in binary form must reproduce the above copyright notice, this list 
'   of conditions and the following disclaimer in the documentation and/or other 
'   materials provided with the distribution.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
' ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
' WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
' IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
' INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
' NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
' PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
' ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
' POSSIBILITY OF SUCH DAMAGE.

Imports System.Collections.Generic
Imports System.Text
Imports System.Web
Imports System.Net
Imports System.IO

Public Class tumblrPostIt

    Public Delegate Sub tumblrEventHandler(ByVal sender As Object, ByVal tea As tumblrEventArgs)
    Public Event tumblrEvent As tumblrEventHandler

    Public Sub activateTumblrEvent(ByVal status As tumblrStatus)
        Dim tumblrArgs As New tumblrEventArgs(status)
        RaiseEvent tumblrEvent(Me, tumblrArgs)
    End Sub

    Public Function postIt(ByVal data As Byte()) As tumblrStatus
        Dim myRequest As HttpWebRequest
        Dim response As HttpWebResponse = Nothing
        Dim newStream As Stream = Nothing
        Dim status As New tumblrStatus()

        myRequest = DirectCast(WebRequest.Create("http://www.tumblr.com/api/write"), HttpWebRequest)

        Dim myProxy As WebProxy
        Dim newUri As Uri

        If (tumblrProxy.useProxy IsNot Nothing) AndAlso CBool(tumblrProxy.useProxy) Then
            Try
                myProxy = New WebProxy()
                newUri = New Uri(tumblrProxy.serverURL & ":" & tumblrProxy.serverPort)
                myProxy.Address = newUri

                If (tumblrProxy.domain IsNot Nothing) AndAlso (tumblrProxy.domain.Length > 0) Then
                    myProxy.Credentials = New NetworkCredential(tumblrProxy.username, tumblrProxy.password, tumblrProxy.domain)
                Else
                    myProxy.Credentials = New NetworkCredential(tumblrProxy.username, tumblrProxy.password)
                End If

                myRequest.Proxy = myProxy

            Catch ex As Exception
                status.message = Convert.ToString(ex.Message)
            End Try
        End If

        myRequest.Credentials = CredentialCache.DefaultCredentials
        myRequest.Method = "POST"
        myRequest.ContentType = "application/x-www-form-urlencoded; charset=utf-8"
        myRequest.ReadWriteTimeout = 600000
        myRequest.Timeout = 600000
        myRequest.ContentLength = data.Length
        Try
            newStream = myRequest.GetRequestStream()
            newStream.WriteTimeout = 600000
            newStream.Write(data, 0, data.Length)

            response = DirectCast(myRequest.GetResponse(), HttpWebResponse)
            status.code = Convert.ToInt32(response.StatusCode)
            status.message = response.StatusDescription
        Catch ex As ProtocolViolationException
            status.message = Convert.ToString(ex.Message)
        Catch ex As NotSupportedException
            status.message = Convert.ToString(ex.Message)
        Catch ex As WebException
            status.message = Convert.ToString(ex.Message)
        Catch exc As Exception
            status.message = Convert.ToString(exc.Message)
        Finally
            If response IsNot Nothing Then
                response.Close()
            End If
            If newStream IsNot Nothing Then
                newStream.Close()
            End If
            activateTumblrEvent(status)
        End Try

        Return status

    End Function

End Class